<?php
namespace app\admin\controller;
use think\Validate;
use think\Loader;
use think\Controller;
use think\Db;
//use app\admin\controller\Base;
use app\admin\model\Product as ProductModel;
use app\admin\model\Insurance_products as Insurance_productsModel; //改名，否则会有两个Admin类，报错
session_start();
header('Content-Type:application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");//允许跨域请求
class Product extends Controller{ //保险产品查询及保险保单
	/*public function _initialize(){ //在类里执行任何方法之前都要执行这个方法
      if(!session('username')){
      	$this->error('非法进入，请先登录！','Login/index');
      }
	}*/

	public function lst(){ //保险产品列表
        if(isset($_POST['tiaojian'])){ //可以实现按保险公司名字或产品名字查询
			$inspect=Db::query("select
			tp_product.bname,
			tp_product.tel,
			tp_product.tname,
			tp_product.bcontent,
			tp_insurance_products.pid,
			tp_insurance_products.pname,
			tp_insurance_products.baofei,
			tp_insurance_products.tese,
			tp_insurance_products.xuzhi,
			tp_insurance_products.shili,
			tp_insurance_products.zongshu,
			tp_insurance_products.bx_percent,
			tp_insurance_products.status
			 from tp_product,tp_insurance_products where tp_insurance_products.bid=tp_product.bid and
             ((tp_product.bname LIKE '%".$_POST['tiaojian']."%') or
              (tp_insurance_products.pname LIKE '%".$_POST['tiaojian']."%') ) ");
        }else{
            $inspect=Db::query("select
            tp_product.bname,
            tp_product.tel,
            tp_product.tname,
            tp_product.bcontent,
            tp_insurance_products.pid,
            tp_insurance_products.pname,
            tp_insurance_products.baofei,
            tp_insurance_products.tese,
            tp_insurance_products.xuzhi,
            tp_insurance_products.shili,
            tp_insurance_products.zongshu,
            tp_insurance_products.bx_percent,
            tp_insurance_products.status
             from tp_product,tp_insurance_products where tp_insurance_products.bid=tp_product.bid");
        }
          $info['status']='ok';
          $info['list']=$inspect;
          return json_encode($info,JSON_UNESCAPED_UNICODE);
	}
 public function insure(){ //投保
		$cid=$_POST['session_cid'];
		$registry=Db::name('broker')->where('cid',$cid)->value('registry');

		if($registry == '1'){
			if(request()->isPost()){
			$data=[
			   'orderid'=>GetRandStr(10), //保单编码
               'baodanhao'=>GetRandStr(10),
			   'cid'=>$cid,
			   'pid'=>$_POST['pid'], //产品编码(需要前台保存并返回)
			   'user'=>input('user'),
			   'user_idcard'=>input('user_idcard'),
			   'user_tel'=>input('user_tel'),
			   'ranqihao'=>input('ranqihao'),
			   'rqtype'=>input('rqtype'),
			   'rqposition'=>input('rqposition'),
			   'term'=>input('term'),//保险期限(1年、2年...5年)
			   'etime'=>time()+(input('term')*31536000),//保险止期
			   'qishu'=>input('qishu'),//保险份数
			   'baofei'=>input('baofei'),//每份保费(需要前台保存并渲染到表单改为不可更改)
			   'zongji'=>input('zongji'),//总保费
			   'rdatetime'=>time(),//录包时间
			];
			$comid=Db::name('broker')->where('cid',$cid)->value('comid');
			$interface=[    //保险接口调用记录
			   'oderid'=>$data['orderid'],
			   'comid'=>$comid,
			   'intername'=>'投保',
			   'dtime'=>time(),
		    ];
            $pay=[    //保单支付信息(假支付)
               'oderid'=>$data['orderid'],
               'name'=>$data['user'],
               'zongji'=>input('zongji'),
               'pay'=>input('zongji'),
               'paymethod'=>'微信',
               'payno'=>GetRandStr(15),
               'paytime'=>time(),
            ];

			$validate = Loader::validate('insure');
			if(!$validate->scene('add')->check($data)){  //应用验证
			   $insure_info=array('message'=>$validate->getError());
               return json_encode($insure_info,JSON_UNESCAPED_UNICODE);
			}

			if(db('policy')->insert($data)){ //助手函数添加(投保成功)
                $insure_info['message']='ok';
                $interface['status']='1';
                db('interface')->insert($interface);//插入接口调用表
                db('pay')->insert($pay);//插入保单支付表
                return json_encode($insure_info,JSON_UNESCAPED_UNICODE);
			}else{
				$interface['status']='2';
                db('interface')->insert($interface);//插入接口调用表
				$insure_info['message']='no';
                return json_encode($insure_info,JSON_UNESCAPED_UNICODE);
			}
		}
	 }else{
	    	$insure_info['message']=="抱歉，您还未通过执业认证";//执业认证未通过
	    	return json_encode($insure_info,JSON_UNESCAPED_UNICODE);
	    }

	}
	public function cancel(){ //撤单列表(撤销第二天0点之前的投保保单)
		$cid=$_POST['session_cid'];
		$time=time();
		$policy=Db::query("select * from tp_policy where cid='".$cid."' and ('".$time."'-rdatetime) <=172800");
        $info['status']='ok';
        $info['list']=$policy;
        return json_encode($info,JSON_UNESCAPED_UNICODE);

	}
	public function cancel_put(){ //提交撤单
		$cid=$_POST['session_cid'];
		$name=Db::name('broker')->where('cid',$cid)->value('name');
        $orderid=$_POST['orderid'];
        $baodanhao=$_POST['baodanhao'];
        $jine=$_POST['jine'];
		$data['orderid']=$orderid;
        $data['baodanhao']=$baodanhao;
		$data['cedanhao']=GetRandStr(15);
		$data['jine']=$jine;
		$data['ctime']=time();
		$data['dxcode']=GetRandStr(5);
		$data['operator']=$name;
		$comid=Db::name('broker')->where('cid',$cid)->value('comid');
			$interface=[    //保险接口调用记录
			   'oderid'=>$data['orderid'],
			   'comid'=>$comid,
			   'intername'=>'撤单',
			   'dtime'=>time(),
		    ];

		if(db('kill')->insert($data)){ //助手函数添加
			    $interface['status']='1';
                db('interface')->insert($interface);//插入接口调用表
                Db::query("update tp_policy set status1 = '4' where cid ='".$cid."' ");//更改保单状态
                $register['message']='yes';
                return json_encode($register,JSON_UNESCAPED_UNICODE);
            }else{
            	$interface['status']='2';
                db('interface')->insert($interface);//插入接口调用表
                $register['message']='no';
                return json_encode($register,JSON_UNESCAPED_UNICODE);
            }
	}
	public function personal_policy(){ //个人销售保单查询
		$cid=$_POST['session_cid'];
		$policy=Db::query("select * from tp_policy where cid='".$cid."' ");
        $info['status']='ok';
        $info['list']=$policy;
        return json_encode($info,JSON_UNESCAPED_UNICODE);
	}
	public function count(){ //统计
		$cid=$_POST['session_cid'];
		$num_singular=Db::query("select count(id) from tp_policy where cid='".$cid."' ");//保单总数
		$num_sales=Db::query("select SUM(zongji) from tp_policy where cid='".$cid."' ");//销售额总数
        $info['num_singular']=$num_singular;
        $info['num_sales']=$num_sales;
        return json_encode($info,JSON_UNESCAPED_UNICODE);
	}

}